package com.zy.cat.common.config;

import java.time.Duration;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
 * Redis 配置
 * @author Link
 * @time 2019-02-25 09:57
 */
@Configuration
public class LettuceRedisConfig {
 
	@Bean
    @ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
    @Scope(value = "prototype")
    public <T>GenericObjectPoolConfig<T> redisPool(){
        return new GenericObjectPoolConfig<T>();
    }
 
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-0")
    public RedisStandaloneConfiguration redisConfig0(){
        return new RedisStandaloneConfiguration();
    }
 
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-1")
    public RedisStandaloneConfiguration redisConfig1(){
        return new RedisStandaloneConfiguration();
    }
 
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-2")
    public RedisStandaloneConfiguration redisConfig2(){
        return new RedisStandaloneConfiguration();
    }
    
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-3")
    public RedisStandaloneConfiguration redisConfig3(){
        return new RedisStandaloneConfiguration();
    }
 
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-4")
    public RedisStandaloneConfiguration redisConfig4(){
        return new RedisStandaloneConfiguration();
    }
 
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-5")
    public RedisStandaloneConfiguration redisConfig5(){
        return new RedisStandaloneConfiguration();
    }
    
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-6")
    public RedisStandaloneConfiguration redisConfig6(){
        return new RedisStandaloneConfiguration();
    }
 
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-7")
    public RedisStandaloneConfiguration redisConfig7(){
        return new RedisStandaloneConfiguration();
    }
 
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-8")
    public RedisStandaloneConfiguration redisConfig8(){
        return new RedisStandaloneConfiguration();
    }
    
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-9")
    public RedisStandaloneConfiguration redisConfig9(){
        return new RedisStandaloneConfiguration();
    }
 
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-10")
    public RedisStandaloneConfiguration redisConfig10(){
        return new RedisStandaloneConfiguration();
    }
 
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-11")
    public RedisStandaloneConfiguration redisConfig11(){
        return new RedisStandaloneConfiguration();
    }
    
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-12")
    public RedisStandaloneConfiguration redisConfig12(){
        return new RedisStandaloneConfiguration();
    }
 
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-13")
    public RedisStandaloneConfiguration redisConfig13(){
        return new RedisStandaloneConfiguration();
    }
 
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-14")
    public RedisStandaloneConfiguration redisConfig14(){
        return new RedisStandaloneConfiguration();
    }
 
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-15")
    public RedisStandaloneConfiguration redisConfig15(){
        return new RedisStandaloneConfiguration();
    }
    
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-16")
    public RedisStandaloneConfiguration redisConfig16(){
        return new RedisStandaloneConfiguration();
    }
 
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-17")
    public RedisStandaloneConfiguration redisConfig17(){
        return new RedisStandaloneConfiguration();
    }
 
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-18")
    public RedisStandaloneConfiguration redisConfig18(){
        return new RedisStandaloneConfiguration();
    }
    
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-19")
    public RedisStandaloneConfiguration redisConfig19(){
        return new RedisStandaloneConfiguration();
    }
 
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.redis-20")
    public RedisStandaloneConfiguration redisConfig20(){
        return new RedisStandaloneConfiguration();
    }
 
    @Bean
    @Primary
    public <T>LettuceConnectionFactory factory0(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig0){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig0, clientConfiguration);
    	return factory;
    }
 
    @Bean
    public <T>LettuceConnectionFactory factory1(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig1){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig1, clientConfiguration);
    	return factory;
    }
 
    @Bean
    public <T>LettuceConnectionFactory factory2(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig2){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig2, clientConfiguration);
    	return factory;
    }
    
    @Bean
    public <T>LettuceConnectionFactory factory3(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig3){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig3, clientConfiguration);
    	return factory;
    }
 
    @Bean
    public <T>LettuceConnectionFactory factory4(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig4){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig4, clientConfiguration);
    	return factory;
    }
    
    @Bean
    public <T>LettuceConnectionFactory factory5(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig5){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig5, clientConfiguration);
    	return factory;
    }
 
    @Bean
    public <T>LettuceConnectionFactory factory6(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig6){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig6, clientConfiguration);
    	return factory;
    }
    
    @Bean
    public <T>LettuceConnectionFactory factory7(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig7){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig7, clientConfiguration);
    	return factory;
    }
 
    @Bean
    public <T>LettuceConnectionFactory factory8(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig8){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig8, clientConfiguration);
    	return factory;
    }
    
    @Bean
    public <T>LettuceConnectionFactory factory9(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig9){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig9, clientConfiguration);
    	return factory;
    }
    
    @Bean
    public <T>LettuceConnectionFactory factory10(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig10){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig10, clientConfiguration);
    	return factory;
    }
    
    @Bean
    public <T>LettuceConnectionFactory factory11(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig11){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig11, clientConfiguration);
    	return factory;
    }
    
    @Bean
    public <T>LettuceConnectionFactory factory12(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig12){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig12, clientConfiguration);
    	return factory;
    }
    
    @Bean
    public <T>LettuceConnectionFactory factory13(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig13){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig13, clientConfiguration);
    	return factory;
    }
    
    @Bean
    public <T>LettuceConnectionFactory factory14(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig14){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig14, clientConfiguration);
    	return factory;
    }
    
    @Bean
    public <T>LettuceConnectionFactory factory15(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig15){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig15, clientConfiguration);
    	return factory;
    }
    
    @Bean
    public <T>LettuceConnectionFactory factory16(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig16){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig16, clientConfiguration);
    	return factory;
    }
    
    @Bean
    public <T>LettuceConnectionFactory factory17(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig17){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig17, clientConfiguration);
    	return factory;
    }
 
    @Bean
    public <T>LettuceConnectionFactory factory18(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig18){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig18, clientConfiguration);
    	return factory;
    }
    
    @Bean
    public <T>LettuceConnectionFactory factory19(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig19){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig19, clientConfiguration);
    	return factory;
    }
    
    @Bean
    public <T>LettuceConnectionFactory factory20(GenericObjectPoolConfig<T> config, RedisStandaloneConfiguration redisConfig20){
    	LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build();
    	LettuceConnectionFactory factory=new LettuceConnectionFactory(redisConfig20, clientConfiguration);
    	return factory;
    }
 
    @Bean
    public <T>RedisTemplate<String,T> redis0(@Qualifier("factory0")LettuceConnectionFactory factory0){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory0);
        return template;
    }
 
    @Bean
    public <T>RedisTemplate<String,T> redis1(@Qualifier("factory1")LettuceConnectionFactory factory1){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory1);
        return template; 
    }
 
    @Bean
    public <T>RedisTemplate<String,T> redis2(@Qualifier("factory2")LettuceConnectionFactory factory2){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory2);
        return template;
    }
    
    @Bean
    public <T>RedisTemplate<String,T> redis3(@Qualifier("factory3")LettuceConnectionFactory factory3){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory3);
        return template; 
    }
 
    @Bean
    public <T>RedisTemplate<String,T> redis4(@Qualifier("factory4")LettuceConnectionFactory factory4){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory4);
        return template;
    }
    
    @Bean
    public <T>RedisTemplate<String,T> redis5(@Qualifier("factory5")LettuceConnectionFactory factory5){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory5);
        return template; 
    }
 
    @Bean
    public <T>RedisTemplate<String,T> redis6(@Qualifier("factory6")LettuceConnectionFactory factory6){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory6);
        return template;
    }
    
    @Bean
    public <T>RedisTemplate<String,T> redis7(@Qualifier("factory7")LettuceConnectionFactory factory7){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory7);
        return template; 
    }
 
    @Bean
    public <T>RedisTemplate<String,T> redis8(@Qualifier("factory8")LettuceConnectionFactory factory8){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory8);
        return template;
    }
    
    @Bean
    public <T>RedisTemplate<String,T> redis9(@Qualifier("factory9")LettuceConnectionFactory factory9){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory9);
        return template; 
    }
    
    @Bean
    public <T>RedisTemplate<String,T> redis10(@Qualifier("factory10")LettuceConnectionFactory factory10){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory10);
        return template;
    }
    
    @Bean
    public <T>RedisTemplate<String,T> redis11(@Qualifier("factory11")LettuceConnectionFactory factory11){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory11);
        return template; 
    }
    
    @Bean
    public <T>RedisTemplate<String,T> redis12(@Qualifier("factory12")LettuceConnectionFactory factory12){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory12);
        return template; 
    }
    
    @Bean
    public <T>RedisTemplate<String,T> redis13(@Qualifier("factory13")LettuceConnectionFactory factory13){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory13);
        return template; 
    }
    
    @Bean
    public <T>RedisTemplate<String,T> redis14(@Qualifier("factory14")LettuceConnectionFactory factory14){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory14);
        return template; 
    }
    
    @Bean
    public <T>RedisTemplate<String,T> redis15(@Qualifier("factory15")LettuceConnectionFactory factory15){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory15);
        return template; 
    }
    
    @Bean
    public <T>RedisTemplate<String,T> redis16(@Qualifier("factory16")LettuceConnectionFactory factory16){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory16);
        return template;
    }
    
    @Bean
    public <T>RedisTemplate<String,T> redis17(@Qualifier("factory17")LettuceConnectionFactory factory17){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory17);
        return template; 
    }
 
    @Bean
    public <T>RedisTemplate<String,T> redis18(@Qualifier("factory18")LettuceConnectionFactory factory18){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory18);
        return template;
    }
    
    @Bean
    public <T>RedisTemplate<String,T> redis19(@Qualifier("factory19")LettuceConnectionFactory factory19){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory19);
        return template; 
    }
    
    @Bean
    public <T>RedisTemplate<String,T> redis20(@Qualifier("factory20")LettuceConnectionFactory factory20){
    	RedisTemplate<String,T> template = getRedisTemplate();
        template.setConnectionFactory(factory20);
        return template;
    }
 
 
	private <T>RedisTemplate<String,T> getRedisTemplate() {
		RedisTemplate<String,T> template = new RedisTemplate<String,T>();
		template.setStringSerializer(new StringRedisSerializer());
		template.setKeySerializer(new StringRedisSerializer());
		template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
		template.setHashKeySerializer(new StringRedisSerializer());
		template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
		template.setDefaultSerializer(new StringRedisSerializer());
		return template;
	}
}
